﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002D5 RID: 725
	internal class RepeatObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06000F89 RID: 3977 RVA: 0x00048128 File Offset: 0x00046528
		public RepeatObservable(T value, int? repeatCount, IScheduler scheduler) : base(scheduler == Scheduler.CurrentThread)
		{
			this.value = value;
			this.repeatCount = repeatCount;
			this.scheduler = scheduler;
		}

		// Token: 0x06000F8A RID: 3978 RVA: 0x00048150 File Offset: 0x00046550
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			//RepeatObservable<T>.<SubscribeCore>c__AnonStorey0 <SubscribeCore>c__AnonStorey = new RepeatObservable<T>.<SubscribeCore>c__AnonStorey0();
			//<SubscribeCore>c__AnonStorey.observer = observer;
			//<SubscribeCore>c__AnonStorey.$this = this;
			//<SubscribeCore>c__AnonStorey.observer = new RepeatObservable<T>.Repeat(<SubscribeCore>c__AnonStorey.observer, cancel);
			//if (this.repeatCount == null)
			//{
			//	return this.scheduler.Schedule(delegate(Action self)
			//	{
			//		<SubscribeCore>c__AnonStorey.observer.OnNext(<SubscribeCore>c__AnonStorey.$this.value);
			//		self();
			//	});
			//}
			//if (this.scheduler == Scheduler.Immediate)
			//{
			//	int num = this.repeatCount.Value;
			//	for (int i = 0; i < num; i++)
			//	{
			//		<SubscribeCore>c__AnonStorey.observer.OnNext(this.value);
			//	}
			//	<SubscribeCore>c__AnonStorey.observer.OnCompleted();
			//	return Disposable.Empty;
			//}
			//int currentCount = this.repeatCount.Value;
			//return this.scheduler.Schedule(delegate(Action self)
			//{
			//	if (currentCount > 0)
			//	{
			//		<SubscribeCore>c__AnonStorey.observer.OnNext(<SubscribeCore>c__AnonStorey.$this.value);
			//		currentCount--;
			//	}
			//	if (currentCount == 0)
			//	{
			//		<SubscribeCore>c__AnonStorey.observer.OnCompleted();
			//		return;
			//	}
			//	self();
			//});
			return null;
		}

		// Token: 0x0400090A RID: 2314
		private readonly T value;

		// Token: 0x0400090B RID: 2315
		private readonly int? repeatCount;

		// Token: 0x0400090C RID: 2316
		private readonly IScheduler scheduler;

		// Token: 0x020002D6 RID: 726
		private class Repeat : OperatorObserverBase<T, T>
		{
			// Token: 0x06000F8B RID: 3979 RVA: 0x00048245 File Offset: 0x00046645
			public Repeat(IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
			}

			// Token: 0x06000F8C RID: 3980 RVA: 0x00048250 File Offset: 0x00046650
			public override void OnNext(T value)
			{
				try
				{
					this.observer.OnNext(value);
				}
				catch
				{
					base.Dispose();
					throw;
				}
			}

			// Token: 0x06000F8D RID: 3981 RVA: 0x0004828C File Offset: 0x0004668C
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000F8E RID: 3982 RVA: 0x000482C4 File Offset: 0x000466C4
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}
		}
	}
}
